package com.easyfun.healthmagicbox.comm;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.comm.json.AlarmClockJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.AlarmClockListJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.JsonWrapper;
import com.easyfun.healthmagicbox.comm.json.PersonQuestionsListJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.AlarmClock;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.sync.CommCallback;

public class AlarmClockUploadTask extends BaseRedoTask implements Runnable{

	String mpersonalID;
	AlarmClock alarmClock;
	
	//For redo task
	public AlarmClockUploadTask(Context context,CommCallback callback)
	{
		super(context,callback);
	}
	
	public AlarmClockUploadTask(Context context,CommCallback callback, String persionID,
								AlarmClock alarmClock)
	{

		super(context,callback);
		this.alarmClock = alarmClock;
		mpersonalID = persionID;
	}

	@Override
	public void run() {
		
		if ((mpersonalID==null)||(mpersonalID.length()==0))
			return;
		JSONObject serializedObj = new JSONObject();
		
			AlarmClockJsonWrapper<JsonWrapper, AlarmClock> wrapper = 
					new AlarmClockJsonWrapper<JsonWrapper, AlarmClock>(alarmClock);
		

		String result = "";
		try {
			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", wrapper.serialize());
			result = super.uploadByHTTP(parmas,ConstantData.AlarmClockUploadURI);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
			return;
		} catch (IOException e) {
			e.printStackTrace();
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
			return;
		} catch (JSONException e) {
			e.printStackTrace();
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
		}
		
		if (ServerResponseCode.parseOf(result)==ServerResponseCode.SUCCESS)
		{
			ccb.setCommResult(this, 
					"", ServerResponseCode.SUCCESS);
		}else
		{
			ccb.setCommResult(this, 
					"", ServerResponseCode.FAILED);
		}
		
	}

	@Override
	public String serializeParam()  throws IllegalArgumentException{
		AlarmClockJsonWrapper<JsonWrapper, AlarmClock> wrapper = 
				new AlarmClockJsonWrapper<JsonWrapper, AlarmClock>(alarmClock);
		String serializedStr = "";
		try {
			serializedStr = wrapper.serialize();
		} catch (JSONException e) {
			e.printStackTrace();
			throw new IllegalArgumentException(e);
		}
		return mpersonalID+BaseRedoTask.ContentsSeperator
				+serializedStr;
	}

	@Override
	public void deserializeParam(String serialstring) throws IllegalArgumentException{
		String[] tmp = serialstring.split(BaseRedoTask.regExHead + BaseRedoTask.ContentsSeperator);
		if (tmp.length == 2)
		{
			mpersonalID = tmp[0];
			try {
				AlarmClockListJsonWrapper<?> wapper = AlarmClockListJsonWrapper
						.getInstance(tmp[1],AlarmClockListJsonWrapper.class);
				List<AlarmClock> alarms = wapper.getMpps();
				if ((alarms!=null) && (alarms.size()>0))
				{
					alarmClock = alarms.get(0);
				}
			} catch (JSONException e) {				
				e.printStackTrace();
				throw new IllegalArgumentException(e);
			} catch (InstantiationException e) {
				e.printStackTrace();
				throw new IllegalArgumentException(e);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				throw new IllegalArgumentException(e);
			}
			return;
		}
		
	}

}
